package awdc;

import java.util.regex.*;


class Command {
    Command Next;
    String CommandName;
    //String[] Param;
    ComParams Params;
    Command(String Name, String ParamString) {
        //System.out.println("Creating command with name="+Name+" and params = "+ParamString);
        CommandName = Name;
        //Сюда надо впихнуть проверку не пустая ли строка параметров
        ////////////////////////////////////////////////////////////
        if (ParamString!=null){
            Pattern Pattern=null;
            Pattern ParPattern=null;
            try {
                ParPattern = ParPattern.compile("[^\\s].*");
            } catch (Exception e) {
                e.printStackTrace();
            }
            Matcher m = ParPattern.matcher(ParamString);
            if (m.matches()){
                Params = new ComParams(ParamString);
                //System.out.println("Commande() --- "+Params.NextP.Param);
            }
            else
                Params = null;
        } else
            Params = null;
        /////////////////////////////////////////////////////////// 
        //Next = null;
    }

	//Метод, возвращающий имя команды
	String getCommandName(){
		return CommandName;
	}
	
	ComParams getParametrs(){
		return Params;
	}
    void showParams(ComParams First){
        if (First!=null){
            System.out.println("\t"+First.Param);
            First=First.NextP;
            showParams(First);
        }
    }

}

class ComParams {
    String Param;
    ComParams NextP;
    ComParams(String ParamString){
        //System.out.println("ComParams() - started constructor");
        this.NextP=null;
        Pattern ParPattern=null;
        try {
            ParPattern = ParPattern.compile("([^\\s]*)(\\s|)(.*|)");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Matcher m = ParPattern.matcher(ParamString);
        //System.out.println(m.matches());
        if (m.matches()) {
        //System.out.println("ComParams(): Found parametr "+m.group(1));
            Param=m.group(1);
            if (Pattern.matches("\\s", m.group(2)))
                this.NextP=new ComParams(m.group(3));

        }
    }
	String getParam(){
		return Param;
	}
	ComParams getNext(){
		return NextP;
	}
}


